import flet as fl
import shutil
import datetime
def main(page:fl.Page):
    page.padding=0
    page.title="快速备份文件"
    def pick_files_result(e: fl.FilePickerResultEvent):
        selected_files.value = (
            ", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
        )
        selected_files.update()
    pick_files_dialog = fl.FilePicker(on_result=pick_files_result)
    selected_files = fl.Text()
    def get_directory_result(e: fl.FilePickerResultEvent):
        directory_path.value = e.path if e.path else "Cancelled!"
        directory_path.update()
    def get_directory_result1(e: fl.FilePickerResultEvent):
        directory_path1.value = e.path if e.path else "Cancelled!"
        directory_path1.update()

    get_directory_dialog = fl.FilePicker(on_result=get_directory_result)
    get_directory_dialog1 = fl.FilePicker(on_result=get_directory_result1)
    directory_path = fl.Text()
    directory_path1 = fl.Text()
    info=fl.Text(value="Ready",size=20,color=fl.colors.YELLOW_700,text_align=fl.TextAlign.CENTER)
    # hide all dialogs in overlay
    page.overlay.extend([pick_files_dialog, get_directory_dialog,get_directory_dialog1])
    def copy_files(e):
        print(directory_path.value)
        print(directory_path1.value)
        if directory_path.value==None or directory_path1.value==None or directory_path.value=="Cancelled!" or directory_path1.value=="Cancelled!":
            info.value="Error!!!请选择文件夹"
            info.color=fl.colors.RED
            page.update()
        elif directory_path.value==directory_path1.value:
            info.value="Error!!!两个文件夹不可相同"
            info.color=fl.colors.RED
            page.update()
        else:
            info.value="开始备份文件"
            info.color=fl.colors.BLUE_ACCENT_400
            page.update()
            now = datetime.datetime.now()
            date_time = now.strftime("%Y-%m-%d-%H-%M-%S")
            final_dir=directory_path1.value+"\Backup\T"+str(date_time)
            shutil.copytree(directory_path.value, final_dir)
            info.value="备份完成"
            info.color=fl.colors.GREEN
            page.update()

    page.add(
        fl.Text(value=""),
        fl.Row(
            [
                fl.ElevatedButton(
                    "需备份的文件夹",
                    icon=fl.icons.FOLDER_OPEN,
                    on_click=lambda _: get_directory_dialog.get_directory_path(),
                    disabled=page.web,
                    style=fl.ButtonStyle(shape=fl.RoundedRectangleBorder(radius=1)),
                ),
                directory_path,
                
            ]
        ),
        fl.Row(
            [
                fl.ElevatedButton(
                    "固定备份文件夹",
                    icon=fl.icons.FOLDER_OPEN,
                    on_click=lambda _: get_directory_dialog1.get_directory_path(),
                    disabled=page.web,
                    style=fl.ButtonStyle(shape=fl.RoundedRectangleBorder(radius=1)),
                ),
                directory_path1,
            ]
        ),
        fl.Column([
            fl.ElevatedButton(
                    "开始备份",
                    icon=fl.icons.COPY_ALL,
                    on_click=copy_files,
                    disabled=page.web,
                    width=page.window.width,
                    style=fl.ButtonStyle(shape=fl.RoundedRectangleBorder(radius=1)),
                ),
            fl.Card(content=info,width=page.window.width,shape=fl.RoundedRectangleBorder(radius=1))
        ])
    )
fl.app(target=main)